package com.xidian.yunwudemo.service.impl;

import com.xidian.yunwudemo.dto.ConfigInfoDto;
import com.xidian.yunwudemo.dto.ServerInfoDto;
import com.xidian.yunwudemo.entities.ConfigInfo;
import com.xidian.yunwudemo.mapper.master.ConfigInfoMapper;
import com.xidian.yunwudemo.mapper.master.ServerInfoMapper;
import com.xidian.yunwudemo.service.ServiceControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ServiceControlServiceImpl implements ServiceControlService {
    @Autowired
    ServerInfoMapper serverInfoMapper;

    @Autowired
    ConfigInfoMapper configInfoMapper;
    @Override
    public ServerInfoDto getServerInfo(String name) {
        return serverInfoMapper.getServerInfoByName(name);
    }

    @Override
    public boolean stopServerOneByOne(String name) {
        return true;
    }

    @Override
    public List<ConfigInfoDto> getConfigByName(String name) {
//        String filePath = "S:\\yunwu-v2\\backend\\src\\main\\resources\\json\\";
        // 主机写法
//        String root = System.getProperty("user.dir");
//        String filePath = root + "\\src\\main\\resources\\json\\";

        // 服务器写法
        List<ConfigInfoDto> ret = configInfoMapper.selectByGroup(name);
//        String filePath = "/home/xiawei/webapps/backend/json/";
//        String fileName = filePath + name + "Config.json";
//        return new ConfigReadUtil().getConfigInfo(fileName);
        return ret;
    }

    @Override
    public List<Integer> authStatus(String name) {
        if(name.equals("adaptive")){
            return Arrays.asList(1,1,1);
        }else if(name.equals("user")){
            return Arrays.asList(1,0,1,1,0,1,1);
        }else {
            return Arrays.asList(1,1);
        }
    }

    @Override
    public boolean saveConfig(List<ConfigInfoDto> configInfos, String group) {
        int flag = configInfoMapper.updateConfig(configInfos,group);
        return flag != 0;
    }

    @Override
    public boolean saveUserConfig(List<ConfigInfo> configInfos) {
        return true;
    }

    @Override
    public boolean saveBioConfig(List<ConfigInfo> configInfos) {
        return true;
    }

    @Override
    public ServerInfoDto getPortByName(String name) {
        String port =  configInfoMapper.getPortInfo("port",name);
        return new ServerInfoDto("172.17.17.2",port);
    }
}
